<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
//jimport('joomla.application.component.modelitem');
jimport('joomla.application.component.modeladmin');

/**
 * Main Model
 */
//class WelliVermittlungModelWelli extends JModelItem
class WelliVermittlungModelWelli extends JModelAdmin
{

    /**
     * @var array messages
     */
    protected $item;

    /**
     * Returns a reference to the a Table object, always creating it.
     *
     * @param       type    The table type to instantiate
     * @param       string  A prefix for the table class name. Optional.
     * @param       array   Configuration array for model. Optional.
     * @return      JTable  A database object
     * @since       2.5
     */
    public function getTable($type = 'Welli', $prefix = 'WelliVermittlungTable', $config = array())
    {
        JTable::addIncludePath(JPATH_COMPONENT . '/tables');
        return JTable::getInstance($type, $prefix, $config);
    }

    /**
     * Method to get the record form.
     *
     * @param       array   $data           Data for the form.
     * @param       boolean $loadData       True if the form is to load its own data (default case), false if not.
     * @return      mixed   A JForm object on success, false on failure
     * @since       2.5
     */
    public function getForm($data = array(), $loadData = true)
    {
        // to test this use:
        // http://localhost/joomla3/index.php?option=com_wellivermittlung&view=arzt&layout=edit&id=3
        // Get the form.
        // This does only seem to work on admin side
        //$form = $this->loadForm('com_wellivermittlung.arzt', 'arzt', array('control' => 'jform', 'load_data' => $loadData));

        // So we have to build the path to the form ourselves. :( There surely is a better solution not found yet.        
        //$form = JForm::getInstance('frmwelli', JPATH_COMPONENT . '/models/forms/welli.xml');
        JForm::addFieldPath(JPATH_COMPONENT_ADMINISTRATOR . DS . 'models' . DS . 'fields');
        $form = $this->loadForm('com_wellivermittlung.welli', 'welli', array('control' => 'jform', 'load_data' => $loadData));

        if (empty($form))
        {
            return false;
        }
        return $form;
    }

    /**
     * Method to get the data that should be injected in the form.
     *
     * @return      mixed   The data for the form.
     * @since       2.5
     */
    protected function loadFormData()
    {
        // Check the session for previously entered form data.
        $data = JFactory::getApplication()->getUserState('com_wellivermittlung.edit.welli.data', array());
        if (empty($data))
        {
            $data = $this->getItem();
        }
        return $data;
    }

    /**
     * Get the message
     * @param  int    The corresponding id of the message to be retrieved
     * @return string The message to be displayed to the user
     */
/*  
    public function getItem($id = 0)
    {
        if (!is_array($this->item))
        {
            $this->item = array();
        }

        if (!isset($this->item[$id]))
        {
            //request the selected id
            $jinput = JFactory::getApplication()->input;
            $welliid = $jinput->get('id', 0, 'INT');

            // Get a TableHelloWorld instance
            $table = $this->getTable();

            // Load the message
            $table->load($welliid);

            // Assign the message
            $this->item[$id] = Array(
                'id' => $welliid,
                'name' => $table->Name,
                'GeschlechtId' => $table->GeschlechtId,
                'Ringnummer' => $table->Ringnummer);
        }

        return $this->item[$id];
    }
*/
}
